import {
  Controller,
  Get,
  Post,
  Body,
  Patch,
  Param,
  Delete,
  Req,
  Put,
  Query
} from '@nestjs/common'
import { AuthService } from './auth.service'
import { CreateAuthDto } from './dto/create-auth.dto'
import { UpdateAuthDto } from './dto/update-auth.dto'
import { NoAuth } from 'src/role/decorators/no-auth.decorator'
import { ApiBody, ApiOperation, ApiParam, ApiTags } from '@nestjs/swagger'
import { FResponse } from 'src/utils/reponse'
@NoAuth()
@ApiTags('权限管理')
@Controller('auth')
export class AuthController {
  constructor(private readonly authService: AuthService) {}

  @Post()
  @ApiOperation({ summary: '创建权限' })
  @ApiBody({ type: CreateAuthDto, description: '权限信息' })
  async create(@Body() createAuthDto: CreateAuthDto) {
    return FResponse.success(await this.authService.create(createAuthDto))
  }
  @Post('all')
  @ApiOperation({ summary: '获取所有权限' })
  @ApiBody({
    description: '权限信息'
  })
  async findAll(
    @Body()
    body: {
      page: number
      pageSize: number
      isDelete?: boolean
    }
  ) {
    return FResponse.success(
      await this.authService.findAll({
        page: +body.page,
        pageSize: +body.pageSize,
        isDelete: body.isDelete
      })
    )
  }

  @Get('menu')
  @ApiOperation({ summary: '获取权限菜单' })
  async findAuthMenu(@Query('isAdminNow') isAdminNow, @Req() req) {
    if (isAdminNow === 'false')
      return FResponse.success(await this.authService.findAuthMenu(3))
    return FResponse.success(await this.authService.findAuthMenu(req.user.role))
  }

  @Get('menu/tree')
  @ApiOperation({ summary: '获取权限菜单树' })
  async findAuthMenuTree() {
    return FResponse.success(await this.authService.findAuthMenu(1))
  }

  @Get(':id')
  @ApiOperation({ summary: '获取单个权限' })
  @ApiParam({ name: 'id', description: '权限id', example: 1 })
  async findOne(@Param('id') id: string) {
    return FResponse.success(await this.authService.findOne(+id))
  }

  @Patch(':id')
  @ApiOperation({ summary: '更新权限' })
  @ApiParam({ name: 'id', description: '权限id', example: 1 })
  @ApiBody({ type: UpdateAuthDto, description: '权限信息' })
  async update(@Param('id') id: string, @Body() updateAuthDto: UpdateAuthDto) {
    return FResponse.success(await this.authService.update(+id, updateAuthDto))
  }

  // 批量删除
  @Delete()
  @ApiOperation({ summary: '批量删除权限' })
  @ApiBody({ type: [Number], description: '权限ID数组' })
  async batchRemove(@Body() ids: number[]) {
    return FResponse.success(await this.authService.remove(ids))
  }

  // 彻底删除
  @Delete('thorough')
  @ApiOperation({ summary: '批量彻底删除权限' })
  @ApiBody({ type: [Number], description: '权限ID数组' })
  async batchThoroughRemove(@Body() ids: number[]) {
    return FResponse.success(await this.authService.thoroughRemove(ids))
  }

  // 批量恢复
  @Put('restore')
  @ApiOperation({ summary: '批量恢复权限' })
  @ApiBody({ type: [Number], description: '权限ID数组' })
  async batchRestore(@Body() ids: number[]) {
    return FResponse.success(await this.authService.restore(ids))
  }
}
